package com.cqndt.disaster.device.common;

import com.cqndt.disaster.device.common.service.DataStatisticsService;
import com.cqndt.disaster.device.common.util.AbstractController;
import com.cqndt.disaster.device.common.util.Result;
import com.cqndt.disaster.device.vo.TabUserVo;
import com.ut.uttools.annos.HandleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created By marc
 * Date: 2019/4/13  9:52
 * Description:数据统计
 */
@RestController
@RequestMapping("data")
@Slf4j
@Api(value = "数据统计", description = "数据统计")
public class DataStatisticsController extends AbstractController {
    @Autowired
    private DataStatisticsService dataStatisticsService;

    @HandleLog("综合统计(灾害点、项目、安装布置点、设备、传感器)")
    @PostMapping("countBaseNum")
    @ApiOperation(value = "综合统计(灾害点、项目、安装布置点、设备、传感器)", notes = "综合统计(灾害点、项目、安装布置点、设备、传感器)")
    public Result countBaseNum() {
        Result result = new Result();
        TabUserVo userVo = getUser();
        log.info("当前用户信息"+userVo);
        userVo.setAreaCode(userVo.getAreaCode());
        userVo.setLevel(userVo.getLevel());
        userVo.setId(userVo.getId());
        Map<String,Object> map = dataStatisticsService.countBaseNum(userVo);
        result.setData(map);
        return result;
    }

    @HandleLog("监测方法（设备类型）统计")
    @PostMapping("countMonitorTypeNum")
    @ApiOperation(value = "监测方法（设备类型）统计", notes = "监测方法（设备类型）统计")
    public Result countMonitorTypeNum() {
        Result result = new Result();
        List<Map<String,Object>> list = dataStatisticsService.countMonitorTypeNum(getUserId());
        result.setData(list);
        return result;
    }

    @HandleLog("害点类型统计")
    @PostMapping("countBasicTypeNum")
    @ApiOperation(value = "害点类型统计", notes = "害点类型统计")
    public Result countBasicTypeNum() {
        Result result = new Result();
        TabUserVo userVo = getUser();
        userVo.setAreaCode(userVo.getAreaCode());
        userVo.setLevel(userVo.getLevel());
        userVo.setId(userVo.getId());
        List<Map<String,Object>> list = dataStatisticsService.countBasicTypeNum(userVo);
        result.setData(list);
        return result;
    }

    @HandleLog("状态统计（1日无数据、2日无数据、3日无数据、4日及以上无数据、正常）")
    @PostMapping("countInstrumentNum")
    @ApiOperation(value = "状态统计（1日无数据、2日无数据、3日无数据、4日及以上无数据、正常）", notes = "状态统计（1日无数据、2日无数据、3日无数据、4日及以上无数据、正常）")
    public Result countInstrumentNum() {
        Result result = new Result();
        Map<String,Object> map = dataStatisticsService.countInstrumentNum(getUserId());
        result.setData(map);
        return result;
    }

    @HandleLog("设备在线离线统计")
    @PostMapping("countDeviceStateNum")
    @ApiOperation(value = "设备在线离线统计", notes = "设备在线离线统计")
    public Result countDeviceStateNum() {
        Result result = new Result();
        List<Map<String,Object>> list = dataStatisticsService.countDeviceStateNum(getUserId());
        result.setData(list);
        return result;
    }

    @HandleLog("设备累计告警统计")
    @PostMapping("countDeviceAlarmNum")
    @ApiOperation(value = "设备累计告警统计", notes = "设备累计告警统计")
    public Result countDeviceAlarmNum() {
        Result result = new Result();
        List<Map<String,Object>> list = dataStatisticsService.countDeviceAlarmNum(getUserId());
        if(!list.isEmpty()){
            for(Map<String,Object> map : list){
                map.put("level_name",map.get("level_name")==null?"0":map.get("level_name"));
                map.put("level",map.get("level")==null?"0":map.get("level"));
                map.put("num",map.get("num")==null?"0":map.get("num"));
            }
        }else {
            String levelName = "红";
            for(int i=1;i<=4;i++){
                Map<String,Object> map = new HashMap<>();
                map.put("level_name",levelName);
                map.put("level",i);
                map.put("num","0");
                list.add(map);
                switch (i){
                    case 2:
                        levelName = "橙";
                        break;
                    case 3:
                        levelName = "黄";
                        break;
                    case 4:
                        levelName = "蓝";
                        break;
                }
            }
        }
        result.setData(list);
        return result;
    }

    @HandleLog("新版在线离线统计")
    @PostMapping("countAllDeviceStateOnline")
    @ApiOperation(value = "新版在线离线统计", notes = "新版在线离线统计")
    public  Result countAllDeviceStateOnline(){
        Result result = new Result();
        Map<String,Object> list = dataStatisticsService.countAllDeviceStateOnline(getUserId());
        result.setData(list);
        return  result;
    }


    @HandleLog("测试接口")
    @PostMapping("countDeviceStateNum1")
    @ApiOperation(value = "测试接口", notes = "测试接口")
    public Result countDeviceStateNum1(Integer page, Integer size) {
        Result result = new Result();
        Result list = dataStatisticsService.countDeviceStateNum1(getUserId(), page, size);
        result.setData(list);
        return result;
    }
}
